<?php
/***************************************************************************
 *
 *   copyright            : (C) 2005 Winds of Storm
 *
 *   $Id: aliases.php,v 1.2 2007/02/14 03:41:02 nonny Exp $
 *
 ***************************************************************************/

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

require("config.php");
$body['class']	= 'row3';
$full_header	= 1;
$skip_header	= 1;
$page_title	= "Manage Chat Aliases";
require($libdir . "init." . $phpEx);
require($libdir . "chat_functs." . $phpEx);

$sid	= $_REQUEST['sid'];
$sql	= "SELECT *
		FROM
			{$dbprefix}sessions AS s,
			{$dbprefix}prefs AS p
		WHERE
			s.session_id='$sid' AND
			s.user_id=p.user_id";
$result	= $db->sql_query($sql);
$rows	= $db->sql_numrows($result);

if ($rows == 0) {
        showFatalError("Session expired, please log back in <a href=index.$phpEx>here</a>.");
}

$row		= $db->sql_fetchrow($result);
$uid		= $row['user_id'];

setDomainText(__FILE__, &$header, $_REQUEST['cpsp']);
showHeader($body, $header);

$template	= new Template($config['TPL_DIR']);

if ($_REQUEST['op'] == "") {
	$template->set_filenames(array('body' => 'aliases_viewbody.tpl'));
	$template->assign_vars(array('HEADER_NAME' => $page_title, 'SID' => $_REQUEST['sid']));
	
	$sql	= "SELECT * 
			FROM
				{$dbprefix}aliases
			WHERE
				user_id={$uid}
			ORDER BY user_alias ASC";
	if (!($result = $db->sql_query($sql))) {
		showFatalError("Could not retrieve chat alias information: $sql");
	} else {
		print("<!-- SQL: $sql -->\n");
	}
	
	while ($row = $db->sql_fetchrow($result)) {
		$vars	= array();
		while (list($k, $v) = each($row)) {
			$vars[strtoupper($k)] = $v;
		}

		if (eregi("http", $vars['USER_AVATAR'])) {
			$vars['AVATAR_IMG'] = "<img src={$vars['USER_AVATAR']}>";
		}
	
		$template->assign_block_vars('aliases', $vars);
	}
	
	$template->pparse('body');
} else if ($_REQUEST['op'] == 'delete') {
	$sql	= "DELETE FROM {$dbprefix}aliases WHERE user_id={$uid} AND alias_id='{$_GET['alias_id']}'";
	if (!($result = $db->sql_query($sql))) {
		showFatalError("Could not update chat alias information: $sql");
	}
	showFatalError("Alias removed successfully!<br />Click <a href=aliases.php?sid={$_REQUEST['sid']}>here</a> to return to managing aliases.","Update Succeeded!","Update Succeeded!");
} else if ($_REQUEST['op'] == 'edit') {
	$template->set_filenames(array('body' => 'aliases_edit.tpl'));
	
	$sql	= "SELECT *
			FROM
				{$dbprefix}aliases
			WHERE
				user_id={$uid} AND
				alias_id={$_GET['alias_id']}
			ORDER BY user_alias DESC";
	if (!($result = $db->sql_query($sql))) {
		showFatalError("Could not retrieve chat alias information: $sql");
	}
	
	$template->assign_vars(array(
		'HEADER_NAME' => $page_title,
		'SID' => $_REQUEST['sid'],
		'ALIAS_ID' => $_REQUEST['alias_id'],
	));

	while ($row = $db->sql_fetchrow($result)) {
		$vars	= array();
		while (list($k, $v) = each($row)) {
			$vars[strtoupper($k)] = $v;
		}
	
		$vars['ALIAS_ID']	= $_REQUEST['alias_id'];
		if (eregi("http", $vars['USER_AVATAR'])) {
			$vars['AVATAR'] = "<center><img src={$vars['USER_AVATAR']}></center>";
		}

		$template->assign_block_vars('aliases', $vars);

		$template->assign_block_vars('aliases.switch_avatar_remote_link', array() );

		$sql2	= "SELECT * FROM {$dbprefix}aliases WHERE user_id={$uid} AND user_avatar != ''";
		if (!($result2 = $db->sql_query($sql2))) {
			showFatalError("Could not retrieve chat alias information: $sql");
		}
	
		while ($row2 = $db->sql_fetchrow($result2)) {
			$vars	= array();
			while (list($k, $v) = each($row2)) {
				$vars[strtoupper($k)] = $v;
			}
			$template->assign_block_vars('aliases.avatars', $vars);
		}
	}

	$template->pparse('body');
} else if ($_REQUEST['op'] == 'update') {
	if ($_REQUEST['avatarremoteurl'] == '') {
		$newavatar		= $_REQUEST['avatar_gallery'];
	} else {
		if (!ereg("://", $_REQUEST['avatarremoteurl'])) {
			$newavatar	= "http://" . $_REQUEST['avatarremoteurl'];
		} else {
			$newavatar	= $_REQUEST['avatarremoteurl'];
		}
	}

	$sql	= "UPDATE {$dbprefix}aliases
			SET
				user_avatar='$newavatar'
			WHERE
				user_id={$uid} AND
				alias_id='{$_POST['alias_id']}'";
	if (!($result = $db->sql_query($sql))) {
		showFatalError("Could not update chat alias information: $sql");
	}

	showFatalError("Alias updated successfully!<br />Click <a href=aliases.php?sid={$_REQUEST['sid']}>here</a> to return to managing aliases.","Update Succeeded!","Update Succeeded!");
} else {
	phpinfo(INFO_VARIABLES);
	showFatalError("Could not determine chat alias operation; exception thrown: $sql");
}

?>
